<template>
  <div class="wrapper">
    <div style="margin: 200px auto;background-color: white;width: 350px; height: 300px; padding: 20px;border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px">登 录</div>

      <el-form ref="form">
        <el-form-item label="用户名" prop="username">
          <el-input style="margin: 5px 0;" size="mini" prefix-icon="el-icon-user" v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input style="margin: 5px 0;" size="mini" prefix-icon="el-icon-lock" v-model="form.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item >
          <el-button style="margin-left: 90px" @click="submit" type="primary" >登录</el-button>
          <el-button type="warning" @click="dialogFormVisible = true">注册</el-button>
        </el-form-item>
      </el-form>

      <div style="margin: 10px 0; tex{ required: true, pattern: /^[1-9]\d*$/, message: '业务编号应为正整数', trigger: 'blur'}t-align: center">

/''
        <el-dialog title="快速注册" :visible.sync="dialogFormVisible">


          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model.number="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm');dialogFormVisible = false">提交</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>

        </el-dialog>

      </div>
    </div>
  </div>

</template>

<script>
import Mock from "mockjs";
import Cookie from "js-cookie";
import { getMenu } from "@/api";
import Axios from 'axios';

export default {
  name: "Login",
  data() {

    var checkUsername = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('用户名不能为空'));
      }else{
        if ( this.ruleForm.username !== '') {
          this.$refs.ruleForm.validateField('username');
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form:{
        username:'',
        password:'',
      },
      dialogTableVisible: false,
      dialogFormVisible: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        username: ''
      },
      rules: {
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
      }
    };
  },
  created: function(){
    this.Get()
  },
  methods: {
    Get() {
      console.log(this._data)
      console.log("调用get")
      this._data.res = Axios.post("http://127.0.0.1:8088/api/user/login").then
      console.log(this._data.res)
    },

    submitForm(formName) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs.ruleForm.resetFields();
    },

    submit()
    {
      //token信息
      const token = Mock.Random.guid()
      //加你个token信息存入cookie用于不同页面的通信
      Cookie.set('token',token)

      //校验通过
      // this.$refs.form.validate((valid) =>{
      //   if(valid){
      //     getMenu(this.form).then(({ data }) => {
      //       console.log(data)
      //     })
      //   }
      // })
      // getMenu()
      //跳转到首页
      this.$router.push('/home')
    }
  }


}
</script>

<style scoped>
/* .wrapper{
  background-image: url("../assets/Title.png") ;
  background-size: cover;
  overflow: hidden;
} */
.wrapper{
    height: 100vh;
    background-image: linear-gradient(to bottom right,#FC466B,#3F5EFB);
    overflow: hidden;
  }
</style>